/* eslint-disable @typescript-eslint/no-var-requires */
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
// 自定义路由
const rewriter = jsonServer.rewriter({
  '/api/*': '/$1'
})
server.use(rewriter)
server.use(middlewares)
// 模拟处理不规则的路由比如获取手机验证码/登录等
server.get('/echo',(req, res) => {
  res.jsonp({ test: '1314'})
})
// 模拟返回数据跟真实返回数的结构一样
// 我们在router上进行包裹
router.render = (req, res) => {
  res.jsonp({
    errno: 0,
    data: {
      list: res.locals.data,
      count: res.locals.data.length
    }
  })
}
server.use(router)
server.listen(3000, () => {
  console.log('JSON Server is running')
})